window.SSEEventSource || (function( window, undefined ) {
	window.SSEEventSource = {
		eventsMap: [],
		register: function(url, callback) {			
			if(typeof(EventSource)!=="undefined") {
						
				var source = new EventSource(url);
			
				source.onmessage=function(event) {
					callback(event);
				};
				
				return source;
			}
			return null;
		},
		bind: function(prop, url, changeCallback) {
			
			if(typeof(SSEEventSource.eventsMap) == "undefined")
				SSEEventSource.eventsMap = [];

			if(typeof(SSEEventSource.eventsMap[prop]) != "undefined") {
				SSEEventSource.eventsMap[prop].close();
			}

			var source = SSEEventSource.register(url, function(event){
				eval("var old_val = " + prop + ";");
				//console.log(event.data);
				var new_val = event.data;
				if(old_val != new_val) {
					changeCallback(new_val);
				}
			});
			
			SSEEventSource.eventsMap[prop] = source;
		}	
	};

})( window );